    //
//  modalMapView.m
//  citipad
//
//  Created by Chih Yuan Liu on 3/9/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "modalMapView.h" 
#import "mapAnnotation.h"
#import "CTPPlacesFetcher.h"
#import "CTPCalloutModel.h"

@implementation modalMapView

@synthesize holderView;
@synthesize spotListView;

CTPCalloutModel * model = nil;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	MKCoordinateRegion newRegion;
    newRegion.center.latitude = latitude;
    newRegion.center.longitude =longitude;
    newRegion.span.latitudeDelta = 0.015;
    newRegion.span.longitudeDelta = 0.015;
	
    [self.map setRegion:newRegion animated:YES];
	
	mapAnnotation * cenAnno = [[mapAnnotation alloc] init];
	[cenAnno setPos:latitude lng:longitude];
	[cenAnno setAnnoTitle:strtitle
				 subtitle:strsubtitle];
	[self.map addAnnotation:cenAnno];
	[self.map selectAnnotation:cenAnno animated:YES];
	[cenAnno release];
	
	CTPPlacesFetcher * fetcher = [[CTPPlacesFetcher alloc] init];
	fetcher.delegate = self;
	[fetcher getDataFromCtp:latitude lng:longitude];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	[model release];
}


- (void)dealloc {
    [super dealloc];
}

- (void)setLocation:(double)lat lng:(double)lng
{
	latitude = lat;
	longitude = lng;
}

-(void) setAnnoTitle:(NSString *)annotitle subtitle:(NSString *)subtitle{
	strtitle= annotitle;
	strsubtitle = subtitle;
}

-(IBAction) close {
	[self dismissModalViewControllerAnimated:YES];
}

@synthesize map;


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	if(model == nil){
		return 0;
	}
	else {
		return [model GetCount];
	}
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
	if(model != nil){
		CTPCallout *callout = [model GetItemAtIndex:indexPath.row];
		cell.textLabel.text = callout.title;
	}
    return cell;
}


/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source.
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	if(model != nil){
		[self.map removeAnnotations:self.map.annotations];
		CTPCallout *callout = [model GetItemAtIndex:indexPath.row];
		MKCoordinateRegion newRegion;
		newRegion.center.latitude = callout.latitude;
		newRegion.center.longitude =callout.longtitude;
		newRegion.span.latitudeDelta = 0.015;
		newRegion.span.longitudeDelta = 0.015;
		
		NSLog(@"DEBUG goto lat:%f, lng:%f", callout.latitude, callout.longtitude);
		
		[self.map setRegion:newRegion animated:YES];
		
		mapAnnotation * cenAnno = [[mapAnnotation alloc] init];
		[cenAnno setPos:callout.latitude lng:callout.longtitude];
		[cenAnno setAnnoTitle:callout.title
					 subtitle:callout.subtitle];
		[self.map addAnnotation:cenAnno];
		[self.map selectAnnotation:cenAnno animated:YES];
		[cenAnno release];
		
	}
}

- (void)placesFetcherBegin:(CTPPlacesFetcher*)sender{}
- (void)placesFetcherEnd:(CTPPlacesFetcher*)sender{}
- (void)placesFetcherSucceeded:(CTPPlacesFetcher*)sender data:(NSData*)data{
	NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

	model = [[CTPCalloutModel alloc ] initWithJSON: responseString];
	NSLog(@"%d", [model GetCount]);
	NSLog(@"%@", responseString); 
	[spotListView reloadData];
}
- (void)placesFetcherFailed:(CTPPlacesFetcher*)sender{}

@end
